import maya.cmds as mc

import OldrosaCommands as orc
reload(orc)

class rigThreeSpanLeg():

    # connect joint if you input
    def connectJnt(*jnt):
            num = len(jnt)
            for i in range( 0,(num-1) ):
                    mc.select(jnt[i+1],r=1)
                    mc.select(jnt[i],tgl=1)
                    mc.parent()
            mc.select(cl=1)
    
    def vis_loc_grp(type,prefix):
            mc.setAttr( prefix+':'+type+'_loc_grp.visibility'  ,  0 )
    
    # Exists locator group.
    def  createSpaceJnt(type , mirror , prefix =''):
    
            if mc.objExists(prefix+':'+type+'_loc_grp'):
                    pass
            else:
                    mc.error(" ---->>>>Please check your files Carefully and find why ''%s'' is missing !<<<<----"%(prefix+':'+type+'_loc_grp')  )
    
            # list all loc in space group.
            if type == 'foot':
                    Drv_List = [prefix+':'+'foot01_loc' , prefix+':'+'foot02_loc' , prefix+':'+'foot03_loc' ,prefix+':'+'Outside_loc' , prefix+':'+'Inside_loc' , prefix+':'+'Toe_loc' , prefix+':'+'Heel_loc']
            elif type == 'leg':
                    Drv_List = [prefix+':'+'leg01_loc' , prefix+':'+'leg02_loc' , prefix+':'+'leg03_loc' , prefix+':'+'legVector_loc' ]
            elif type == 'threeSpan':
                    Drv_List = [prefix+':'+'leg01_loc' , prefix+':'+'leg02_loc' , prefix+':'+'leg03_loc' , prefix+':'+'leg04_loc', prefix+':'+'legVector_loc' ,  prefix+':'+'leg_globalVector_loc']
    
            # set the joint names as create from  space locator.
            Jnt_List = []
            for x in Drv_List:
    
                    if prefix == '':
                            jntName = x.replace('_loc','_drv')
                    else:
                            editName = x.replace('_loc','_drv')
                            jntName = editName.replace(':' , '_')
    
                    Jnt_List.append(jntName)
                    mc.select(cl=True)
                    mc.joint(name=jntName)
                    mc.parentConstraint(x,jntName,mo=False)
                    mc.delete(cn=True)
    
    
    
    
    #-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    
            #connect the joints and rectify joint's LRA.
            if type == 'foot':
                    mc.makeIdentity( Jnt_List[0] , apply=True , t=0 , r=1 , s=0 , n=0 )
                    connectJnt( Jnt_List[0] , Jnt_List[1] , Jnt_List[2] )
                    connectJnt(Jnt_List[3] , Jnt_List[4] , Jnt_List[5],Jnt_List[6] )
                    orc.rectifyJnt(Jnt_List[1])
                    orc.addNewAttr(Jnt_List[0],1,0.01,100,'crvscale')
                    glscale = mc.getAttr(prefix+':'+'foot_loc_grp.globalScale')
                    mc.setAttr( Jnt_List[0]+'.crvscale' , glscale )
    
                    if mirror == 'on' and 'Left' in prefix:
                            mc.mirrorJoint(Jnt_List[0] , mirrorYZ=True , mirrorBehavior=True , searchReplace=('Left', 'Right') )
                            mc.mirrorJoint(Jnt_List[3] , mirrorYZ=True , mirrorBehavior=True , searchReplace=('Left', 'Right') )
    
    
                    elif mirror == 'on' and 'left' in prefix:
                            mc.mirrorJoint(Jnt_List[0] , mirrorYZ=True , mirrorBehavior=True , searchReplace=('left', 'right') )
                            mc.mirrorJoint(Jnt_List[3] , mirrorYZ=True , mirrorBehavior=True , searchReplace=('left', 'right') )
                    elif mirror == 'off':
                            pass
    
    #-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    
    
            elif type == 'leg':
                    connectJnt( Jnt_List[0] , Jnt_List[1] , Jnt_List[2] )
                    orc.rectifyJnt(Jnt_List[0])
                    orc.addNewAttr(Jnt_List[0],1,0.01,100,'crvscale')
                    glscale = mc.getAttr(prefix+':'+'leg_loc_grp.globalScale')
                    mc.setAttr(Jnt_List[0]+'.crvscale' , glscale )
    
                    if mirror == 'on' and 'Left' in prefix:
                            mc.mirrorJoint(Jnt_List[0] , mirrorYZ=True , mirrorBehavior=True , searchReplace=('Left', 'Right') )
                            mc.mirrorJoint(Jnt_List[3] , mirrorYZ=True , mirrorBehavior=False , searchReplace=('Left', 'Right') )
                    elif mirror == 'on' and 'left' in prefix:
                            mc.mirrorJoint(Jnt_List[0] , mirrorYZ=True , mirrorBehavior=True , searchReplace=('left', 'right') )
                            mc.mirrorJoint(Jnt_List[3] , mirrorYZ=True , mirrorBehavior=False , searchReplace=('left', 'right') )
                    elif mirror == 'off':
                            pass
    
    #-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    
            elif type == 'threeSpan':
                    connectJnt( Jnt_List[0] , Jnt_List[1] , Jnt_List[2] , Jnt_List[3] )
                    orc.rectifyJnt(Jnt_List[0])
                    orc.addNewAttr(Jnt_List[0],1,0.01,100,'crvscale')
                    glscale = mc.getAttr(prefix+':'+'foot_loc_grp.globalScale')
                    mc.setAttr( Jnt_List[0]+'.crvscale' , glscale )
                    glvecjnt = prefix+'_legVector_drv'
                    if mirror == 'on' and 'Left' in prefix:
                            mc.mirrorJoint(Jnt_List[0] , mirrorYZ=True , mirrorBehavior=True , searchReplace=('Left', 'Right') )
                            mc.mirrorJoint(Jnt_List[4] , mirrorYZ=True , mirrorBehavior=True , searchReplace=('Left', 'Right') )
                            mc.mirrorJoint(Jnt_List[5] , mirrorYZ=True , mirrorBehavior=True , searchReplace=('Left', 'Right') )
                            #mc.mirrorJoint(glvecjnt , mirrorYZ=True , mirrorBehavior=True , searchReplace=('Left', 'Right') )
                    elif mirror == 'on' and 'left' in prefix:
                            mc.mirrorJoint(Jnt_List[0] , mirrorYZ=True , mirrorBehavior=True , searchReplace=('left', 'right') )
                            mc.mirrorJoint(Jnt_List[4] , mirrorYZ=True , mirrorBehavior=True , searchReplace=('left', 'right') )
                            mc.mirrorJoint(Jnt_List[5] , mirrorYZ=True , mirrorBehavior=True , searchReplace=('left', 'right') )
                            #mc.mirrorJoint(glvecjnt , mirrorYZ=True , mirrorBehavior=True , searchReplace=('left', 'right') )
                    elif mirror == 'off':
                            pass
    
    #-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
            vis_loc_grp(type,prefix)
    
    #createSpaceJnt('leg' , 'off' , 'left')
    #createSpaceJnt('foot' , 'off' , 'left')
    